package action.ManagerAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import service.*;
import vo.*;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin on 2017/5/7.
 */
public class PurchaseAction extends ActionSupport {

    @Autowired
    private CroPurchaseService croPurchaseService;
    @Autowired
    private CroCustomerService croCustomerService;
    @Autowired
    private CroBuycarsService croBuycarsService;
    @Autowired
    private CroAddressService croAddressService;
    @Autowired
    private SysCountryService sysCountryService;
    @Autowired
    private SysProvienceService sysProvienceService;
    @Autowired
    private SysCitysService sysCitysService;

    private List<CroPurchaseVo> lists;
    private List<CroBuycars> buycars;
    private List<BuyCargoodsVo> goodsdetails;
    private CroPurchaseVo purchase;
    private CroPurchase cpurchase;
    private String[] id;
    private String title;
    private String customerId;
    private String addressId;
    private String customerName;
    private String status;
    private String successResult;
    private String jsonAddress;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;
    private Map condition;

    /**
     * 修改更新订单信息
     *
     */
    @RequiresPermissions("user:update")
    public String update(){
        CroPurchase croPurchase = croPurchaseService.findById(cpurchase.getId());
        croPurchase.setPaytype(cpurchase.getPaytype());
        croPurchase.setOrderStatus(cpurchase.getOrderStatus());
        croPurchase.setOrderType(cpurchase.getOrderType());
        croPurchase.setNotes(cpurchase.getNotes());
        croPurchase.setCroAddress(croAddressService.findAddressById(addressId));
        if (croPurchaseService.updatePurchase(croPurchase)){
            successResult = "更新成功！";
        }else {
            successResult = "更新失败！";
        }
        return SUCCESS;
    }

    /**
     * 查询订单用户的地址信息
     * @return json
     */
    @RequiresPermissions("user:select")
    public String listAddress(){
        CroCustomer customer = croCustomerService.FindCustomerById(customerId);
        List<CroAddress> croAddresses = croAddressService.listAddressByCustomer(customer);
        List<CroAddressVo> addressVos = new ArrayList<CroAddressVo>();
        for (CroAddress address: croAddresses){
            CroAddressVo addressVo = new CroAddressVo();
            addressVo.setId(address.getId());
            addressVo.setCustomerId(customerId);
            addressVo.setConsignee(address.getConsignee());
            addressVo.setCountry(address.getCountry().getName());
            addressVo.setProvience(address.getProvience().getName());
            addressVo.setCity(address.getCity().getName());
            addressVo.setAddress(address.getAddress());
            addressVo.setPostcode(address.getPostcode());
            addressVo.setPhone(address.getPhone());
            addressVos.add(addressVo);
        }
        jsonAddress = JSON.toJSONString(addressVos);
        return SUCCESS;
    }

    /**
     * 后台修改订单状态
     */
    @RequiresPermissions("user:update")
    public String done(){
        CroPurchase croPurchase = croPurchaseService.findById(id[0]);
        croPurchase.setOrderStatus("4");
        if (croPurchaseService.updatePurchase(croPurchase)){
            successResult = "修改成功！";
        }else {
            successResult = "修改失败！";
        }
        return SUCCESS;
    }

    /**
     * 后台管理员删除功能
     */
    @RequiresPermissions("user:delete")
    public String delete(){
        if (croPurchaseService.delPurchase(id)){
            successResult = "删除成功！";
        }else {
            successResult = "删除失败！";
        }
        return SUCCESS;
    }

    /**
     * 获取所有的订单信息
     */
//    public String lists() {
//        setLists(croPurchaseService.lists());
//        return "lists";
//    }

    @RequiresPermissions("user:select")
    public String lists() {

        Pagination<CroPurchaseVo> page = new Pagination<CroPurchaseVo>();

        if (pageIndex == 0)//前台没有传值的情况
        {
            page = croPurchaseService.listsPage(1,pageSize);

        } else {
            page = croPurchaseService.listsPage(pageIndex,pageSize);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        setLists(page.getItems());
        return "lists";
    }

    /**
     * 获取status状态订单信息
     */
    @RequiresPermissions("user:select")
    public String listByStatus() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderStatus",status);
        setLists(croPurchaseService.listByProperty(map));
        return "lists";
    }

    /**
     * 通过id获取订单详情(包含订单内物品信息)
     * @return
     */
    @RequiresPermissions("user:select")
    public String detail() {
        setPurchase(croPurchaseService.findByIdExt(id[0]));
        buycars = croBuycarsService.listByPurchase(croPurchaseService.findById(id[0]));
        goodsdetails = new ArrayList<>();
        for(CroBuycars croBuycars : buycars){
            BuyCargoodsVo buyCargoodsVo=new BuyCargoodsVo();
            ParseBuycarVo parseBuycarVo= JSON.parseObject(croBuycars.getGoodsdetail(),ParseBuycarVo.class);
            buyCargoodsVo.setId(croBuycars.getId());
            buyCargoodsVo.setName(parseBuycarVo.getTitle());
            StringBuffer sb=new StringBuffer();
            for(GoodsPropertiesNameVo goodsdetail:parseBuycarVo.getPropertiesName()){
                sb.append(goodsdetail.getPropName()+":"+goodsdetail.getPropValueName()+",  ");
            }
            buyCargoodsVo.setDescrib(sb.toString());
            buyCargoodsVo.setPhoto(parseBuycarVo.getPicUrl());
            buyCargoodsVo.setPrice(parseBuycarVo.getPrice());
            buyCargoodsVo.setActprice(croBuycars.getActualprice());
            buyCargoodsVo.setNumbers(croBuycars.getNumbers());
            buyCargoodsVo.setSumprice(croBuycars.getSumprice());
            goodsdetails.add(buyCargoodsVo);
        }

        return "detail";
    }

    /**
     * 多属性订单搜索
     */
    @RequiresPermissions("user:select")
    public String search() {
        Map<String, Object> maps = new HashMap<String, Object>();
        boolean flag = false;
        if (customerName!=null && customerName.length()>0){
            flag = true;
            CroCustomer co = croCustomerService.FindCustomerByName(customerName);
            if (purchase==null){
                purchase = new CroPurchaseVo();
            }
            purchase.setCroCustomer(co);
        }
        if (purchase!=null) {
            Field[] cf = purchase.getClass().getDeclaredFields();
            try {
                for (Field f : cf) {
                    f.setAccessible(true);
                    if (f.get(purchase) != null && !f.get(purchase).equals("")
                            && !String.valueOf(f.get(purchase)).equals("0.0")) {
                        maps.put(f.getName(), f.get(purchase));
                    }
                }
            } catch (IllegalAccessException e) {
                System.out.println("map转换失败!");
                e.printStackTrace();
            }
        }

        Pagination<CroPurchaseVo> page = new Pagination<CroPurchaseVo>();

        if (pageIndex == 0)//前台没有传值的情况
        {
            page = croPurchaseService.listPageByProperty(maps,1,pageSize);

        } else {
            page = croPurchaseService.listPageByProperty(maps,pageIndex,pageSize);
        }
        this.pageCount = (int) page.getPagesCount();
        this.pageIndex = (int) page.getCurIndex();
        if (flag){
            maps.remove("croCustomer");
        }
        setCondition(maps);
        setLists(page.getItems());
        return "search";
    }

    public String[] getId() {
        return id;
    }

    public void setId(String[] id) {
        this.id = id;
    }

    public List<CroPurchaseVo> getLists() {
        return lists;
    }

    public void setLists(List<CroPurchaseVo> lists) {
        this.lists = lists;
    }

    public CroPurchaseVo getPurchase() {
        return purchase;
    }

    public void setPurchase(CroPurchaseVo purchase) {
        this.purchase = purchase;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public Map getCondition() {
        return condition;
    }

    public void setCondition(Map condition) {
        this.condition = condition;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public List<CroBuycars> getBuycars() {
        return buycars;
    }

    public void setBuycars(List<CroBuycars> buycars) {
        this.buycars = buycars;
    }

    public List<BuyCargoodsVo> getGoodsdetails() {
        return goodsdetails;
    }

    public void setGoodsdetails(List<BuyCargoodsVo> goodsdetails) {
        this.goodsdetails = goodsdetails;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public String getJsonAddress() {
        return jsonAddress;
    }

    public void setJsonAddress(String jsonAddress) {
        this.jsonAddress = jsonAddress;
    }

    public String getAddressId() {
        return addressId;
    }

    public void setAddressId(String addressId) {
        this.addressId = addressId;
    }

    public CroPurchase getCpurchase() {
        return cpurchase;
    }

    public void setCpurchase(CroPurchase cpurchase) {
        this.cpurchase = cpurchase;
    }
}
